[TOC]

Size Vector

This page explains the concept and terminology related to array size.

An array is a collection of data organized on a rectangular grid that can span across multiple dimensions. Except function handles, data of all types are stored in arrays. For examples,

Dimension Length

Given an array A, the length of the i-th dimension is given by size(A, i). This is illustrated in the figure below, where A has 3 dimensions with lengths greater than one.

An array of 3 non-singleton dimensions

It is important to understand that SIMO sees this as an array with more than 3 dimensions, where 4th and higher dimensions all have unit lengths. That is why, in the example below, calling size(A, i) with i greater than 3 returns all ones:

Input
A = ones(2,3,4);
for i = 1:6
    size(A, i)
end
Output
ans = 
 2.0000

ans = 
 3.0000

ans = 
 4.0000

ans = 
 1.0000

ans = 
 1.0000

ans = 
 1.0000

Singleton

A dimension having a unit length is called a singleton dimension. In the above example, the -th dimension with are singleton. In fact, any array has infinite number of trailing singleton dimensions. For illustrations, consider the following examples:

Compatible Sizes

Given two arrays, namely, A and B. When a binary operation is performed on the d-th dimension, at least one of the following conditions must hold. Otherwise, a size-mismatch error will be thrown.

  1. size(A, d) == size(B, d),
  2. size(A, d) = 1
  3. size(B, d) = 1

The first condition ensures that each element of A can be operated on the element of B at the same position. If A and B have different lengths in the d-th dimension, one of them should have a unit length along the d-th dimension (i.e., the second and third conditions), so that the dimension can be expanded to match the length of the other array.

Definition

If at least one of the above 3 conditions holds for each d in 1, 2, ..., max(ndims(A), ndims(B)), then A and B are said to have compatible sizes. If two arrays have compatible sizes, a binary operation can be performed on these arrays.

For examples, each pair of the following arrays has compatible sizes:

size

The function size returns the "size vector" of an array. More precisely, by size vector, we mean

  1. the vector if the array is empty,
  2. the vector if the array is a scalar,
  3. the vector if the array is a column vector,
  4. the vector if the array is a row vector,
  5. the vector if otherwise, where denotes the length of the th dimension and is the largest integer such that the th dimension is non-singleton.

This definition is illustrated by the following examples:

ndims

The number returned by ndims(A) is commonly referred to as the "number of dimensions" of A. More precisely,

length

The length of the i-th dimension refers to size(A, i), i.e., the dimension length. It should be noted that the length of an array means the length of the longest dimension of the array. The length of an array can be obtained by the function length(). For example, length(ones(2,3,4)) gives 4.

numel

The number of elements in the array A can be obtained by numel(A). For examples, the numel of an empty array is zero, and the numel of a scalar is 1.